package com.geeaks.learn.spider;

import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.geeaks.learn.spider.bo.Entity;
import com.geeaks.learn.spider.bo.Result;
import com.google.common.collect.Maps;

public class Spider {
	
	static String loginUrl = "https://ops.qudian.com/api/v2/token/obtain/";
	
	static String queryUrl = "https://ops.qudian.com/api/v2/query-sql/query/";
	
	static String email = "gaoxiang@qudian.com";
	
	static String password = "uWQJBkRp3LKy";
	
	public static void main(String[] args) throws Exception {
		execute();
	}

	private static void execute() throws Exception {
		Map<String, String> loginMap = Maps.newHashMap();
		loginMap.put("email", email);
		loginMap.put("password", password);
		String tokenResult = HttpClientSender.sendHttpPost(loginUrl, JSON.toJSONString(loginMap));
		String token = JSON.parseObject(tokenResult).getJSONObject("data").getString("token");
		Map<String, String> data2 = Maps.newHashMap();
		data2.put("sql", "select * from areas limit 2");
		data2.put("db_id", "788");
		//1html 3csv 4json 5xls
		data2.put("query_type", "4");
		data2.put("favoriteSql", "");
		Map<String, String> headers = Maps.newHashMap();
		headers.put("authorization", "JWT " + token);
		String resultStr = HttpClientSender.sendHttpPost(queryUrl, JSON.toJSONString(data2), headers);
		Result result = JSON.parseObject(resultStr, Result.class);
		List<Entity> dataList = result.getData();
		for(Entity entity : dataList) {
			List<Map<String, String>> data2List = entity.getData();
			for(Map<String, String> map : data2List) {
				System.out.println(JSON.toJSONString(map));
			}
		}
	}
}
